/*
 *  $Id: Demon.cs 1046 2010-05-18 12:17:04Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

using System;

namespace Ragima.Races {
    public class Demon: Race {
        public Demon(): base("demon") {}

        public override void SetNaturalAffects(CreatureCard card, ICollector<Affect> affects) {
            affects.Add<DemonPactStarter>();
            affects.Add(new Affects.AntiFlag<Affects.Reincarnation>());
        }
        public override void SetAbilities(CreatureCard card, ICollector<AbilityCard> abilities) {
            // TODO: заменить добавление способности
            abilities.Add(CardManager.GetAbility("001_n_a_demon_pact"));
        }

        public static void ExecutePact(Creature demon) {
            DemonPactStarter starter = demon.GetAffect<DemonPactStarter>(true);
            if (starter != null) starter.ExecutePact(demon);
        }

        private class DemonPactStarter: Affect {
            private static readonly Languages.Phrase ContractOpeningName = new Languages.Phrase("race.demon.contract_opening.affect");
            private static readonly Languages.Phrase ContractOpeningAction = new Languages.Phrase("race.demon.contract_opening.action");
            private static readonly Languages.Phrase ContractOpeningTake = new Languages.Phrase("race.demon.contract_opening.take");
            private static readonly Languages.Phrase ContractOpeningPay = new Languages.Phrase("race.demon.contract_opening.pay");

            private Affects.DemonPact _pact;

            public DemonPactStarter() {
                _pact = null;
            }

            public override string Id {
                get { return "demon.contract_opening";  }
            }
            public override Languages.Phrase Name {
                get { return ContractOpeningName; }
            }

            public override void SetAffect() {
                base.SetAffect();
                LocalSummary.OnCreatureSummon += DoSummon;
                LocalSummary.OnDeath += DoDeath;
            }
            public override void ResetAffect() {
                base.ResetAffect();
                LocalSummary.OnCreatureSummon -= DoSummon;
                LocalSummary.OnDeath -= DoDeath;
            }

            public void ExecutePact(Creature demon) {
                if (_pact == null) return;
                Player owner = _pact.Owner.Owner;
                DemonCreatureCard card = demon.Card as DemonCreatureCard;
                demon.InstantDeath();
                Game.Current.DoDeath();
                if (card != null) card.ExecutePact(owner);
            }

            private void DoSummon(Creature demon) {
                if (_pact != null) return;
                DemonCreatureCard card = demon.Card as DemonCreatureCard;
                if (card == null) return;
                _pact = new Affects.DemonPact(demon, card.PactValue);
                demon.Owner.AddAffect(_pact);
                Game.Current.Log.Add(ContractOpeningAction, demon, demon.Owner);
            }

            private void DoDeath(AbstractObject obj) {
                DoDemonPact();
            }

            private void DoDemonPact() {
                if (_pact == null) return;
                Player player = _pact.Owner.Owner;
                Game.Current.Log.Add(ContractOpeningTake, Owner);
                int payed = Math.Min(_pact.Value, player.GetMana(ElementType.Darkness));
                Game.Current.Log.Add(ContractOpeningPay, player, payed);
                player.DecrementMana(ElementType.Darkness, payed);
                payed = _pact.Value - payed;
                if (payed > 0)
                    player.DamageDirectBy(NewDamage(DamageType.Darkness, payed), false);
                player.RemoveAffect(_pact);
                _pact = null;
            }
        }
    }
}

namespace Ragima {
    public abstract class DemonCreatureCard: CreatureCard {
        public int PactValue;

        public DemonCreatureCard(string id): base(id) {
            SetRace<Races.Demon>();
            PactValue = 1;
        }

        protected override int GetWeight() {
            int weight = base.GetWeight();
            weight += PactValue * WeightCoeff.DemonPactCost.GetWeight(ElementType.Darkness.GetRelationTo(Element));
            return weight;
        }

        public override CardPlace PlaceAfterDeath { get { return CardPlace.Deck2; } }

        public virtual void ExecutePact(Player owner) {}
    }
}
